﻿using System;
using System.Windows.Input;
using Xamarin.Essentials;
using Xamarin.Forms;
using Capstone.Views;
using Capstone.ViewModels;
using Capstone.Models;
using Capstone.Services;
using System.Diagnostics;

using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;
using System.IO;
using System.Text;
using System.Reflection;
using System.Net.Http;
using Newtonsoft.Json;
using System.Configuration;
using Capstone.Properties;

namespace Capstone.ViewModels
{
    public class AboutViewModel : BaseViewModel
    {
        public Command LogoutCommand { get; }
        public string logInEmail;
        public static Boolean Admin = true;
        public AboutViewModel()
        {
            Title = "About";
            OpenWebCommand = new Command(async () => await Browser.OpenAsync("https,//aka.ms/xamarin-quickstart"));

            if (Application.Current.Properties.ContainsKey("Name") && Application.Current.Properties.ContainsKey("Email"))
            {
                Name = Convert.ToString(Application.Current.Properties["Name"]);
                Email = Convert.ToString(Application.Current.Properties["Email"]);
                logInEmail = Email;

            }
            else
            {
                logInEmail = "no email";
            }
            
            LogoutCommand = new Command(OnLogout);
            checkEmails();
            //checkAdmins();
        }

        public ICommand OpenWebCommand { get; }

        public void OnLogout()
        {
            Name = string.Empty;
            Email = string.Empty;

            Application.Current.Properties.Clear();

            Application.Current.MainPage = new AppShell();
        }

        public async void checkAdmins()
        {
            HttpClient client = new HttpClient();

            var values = new Dictionary<string, string>
            {
                {"token", "1AFB912FDDACCD1E84753275DDBBB421"},
                {"content", "userRoleMapping"},
                {"format", "json"},
                {"returnFormat", "json" }
            };

            

            var content = new FormUrlEncodedContent(values);
            Console.WriteLine("ADMIN_CONTENT: " + content);
            var response = await client.PostAsync("https://redcap.utu.fi/api/", content);
            Console.WriteLine("ADMIN_RESPONSE: " + response);
            var fullResponseString = await response.Content.ReadAsStringAsync();
            Console.WriteLine("ADMIN_FULLRESPONSESTRING: " + fullResponseString);

            if (fullResponseString.Contains(logInEmail))
            {
                Admin = true;
            }
            else
            {
                Admin = false;
            }
        }

        public async void checkEmails()
        {
            HttpClient client = new HttpClient();


            //var values = new Dictionary<string, string>
            //    {
            //        { "token", "540E97741AA3EFF280E55B5C9DB1CD6D" },
            //        { "content", "record" },
            //        { "action", "import" },
            //        { "format", "json" },
            //        { "overwriteBehavior", "normal" },
            //        { "forceAutoNumber", "false" },
            //        { "data",  data},
            //        { "returnContent", "count" },
            //        {"returnFormat", "json" }
            //    };

            var values = new Dictionary<string, string>
            {
                {"token", "1AFB912FDDACCD1E84753275DDBBB421"},
                {"content", "record"},
                {"action", "export"},
                {"format", "json"},
                {"type", "eav"},
                {"csvDelimiter", ""},
                {"fields[0]", "record_id"},
                {"fields[1]", "stool_quality_study_name"},
                {"fields[2]", "stool_diary_time"},
                {"fields[3]", "stool_quality_days"},
                {"fields[4]", "stool_quality_questions"},
                {"fields[5]", "part_emails_stool"},
                {"fields[6]", "gut_symptom_diary_name"},
                {"fields[7]", "gut_diary_time"},
                {"fields[8]", "gut_symptom_diary_length"},
                {"fields[9]", "gut_symptom_diary_frequency"},
                {"fields[10]", "gut_symptom_diary_symptoms"},
                {"fields[11]", "gut_symptom_diary_other_symptoms"},
                {"fields[12]", "gut_symptom_diary_sleep_symptoms"},
                {"fields[13]", "part_emails"},
                {"rawOrLabel", "label"},
                {"rawOrLabelHeaders", "label"},
                {"exportCheckboxLabel", "false"},
                {"exportSurveyFields", "false"},
                {"exportDataAccessGroups", "false"},
                {"returnFormat", "json" }
            };

            var content = new FormUrlEncodedContent(values);
            //Console.WriteLine("CONTENT: " + content);
            var response = await client.PostAsync("https://redcap.utu.fi/api/", content);
            //Console.WriteLine("RESPONSE: " + response);
            var fullResponseString = await response.Content.ReadAsStringAsync();
            Console.WriteLine("fullResponseString: " + fullResponseString);

            string emailList = fullResponseString.Substring(fullResponseString.IndexOf("part_emails") + 22);
            Console.WriteLine("emailList1: " + emailList);
            string leftOverResponseString = emailList.Substring(emailList.IndexOf(".com\"}") + 4);
            Console.WriteLine("leftOverResponseString: " + leftOverResponseString);
            emailList = emailList.Substring(0, emailList.IndexOf(".com\"}") + 4);
            Console.WriteLine("emailList2: " + emailList);




            if (//s == "bill@gmail.com"
                    emailList.Contains(logInEmail)
                    )
            {
                //create new item to items in MockDataStore based on the responseString parameters;

                string id = "";
                string name = "";
                DatePicker time = new DatePicker();
                time.Date = DateTime.Now;
                DateTime dT = time.Date;
                int length = 0;
                string freq = "";
                string symptoms = "";
                string other = "";
                string sleep = "";
                string desc = "";
                string replacer = "";
                int timeDate = 0;
                int timeMonth = 0;
                int timeYear = 0;
                string sqq = "";
                if (fullResponseString.Contains("gut_symptom_diary_name") && fullResponseString.Contains("stool_quality_study_name"))
                {
                    int gut = fullResponseString.IndexOf("gut_symptom_diary_name");
                    int stool = fullResponseString.IndexOf("stool_quality_study_name");
                    Console.WriteLine("gut: " + gut + "stool: " + stool);
                    if (gut < stool)
                    {
                        desc = Properties.Resources.GutDescription;
                        Console.WriteLine("works: " + fullResponseString);

                        string field = fullResponseString.Substring(fullResponseString.IndexOf("record_id") + 11 + 9);
                        field = field.Substring(0, field.IndexOf("}") - 1);
                        id = field;
                        Console.WriteLine("id: " + id);

                        field = fullResponseString.Substring(fullResponseString.IndexOf("gut_symptom_diary_name") + 11 + 22);
                        field = field.Substring(0, field.IndexOf("}") - 1);
                        name = field;
                        Console.WriteLine("name: " + name);

                        field = fullResponseString.Substring(fullResponseString.IndexOf("gut_diary_time") + 11 + 14);
                        field = field.Substring(0, field.IndexOf("}") - 1);
                        Console.WriteLine("time1: " + field);

                        replacer = field.Substring(0, field.IndexOf("-"));
                        Console.WriteLine(replacer);

                        timeYear = Int32.Parse(replacer);

                        replacer = field.Substring(5, 2);
                        Console.WriteLine(replacer);

                        timeMonth = Int32.Parse(replacer);

                        replacer = field.Substring(field.LastIndexOf("-") + 1);
                        Console.WriteLine(replacer);

                        timeDate = Int32.Parse(replacer);

                        dT = new DateTime(timeYear, timeMonth, timeDate);
                        Console.WriteLine("time4: " + dT);
                        time.Date = dT;
                        Console.WriteLine("time5: " + time.Date);

                        field = fullResponseString.Substring(fullResponseString.IndexOf("gut_symptom_diary_length") + 11 + 24);
                        field = field.Substring(0, field.IndexOf("}") - 1);
                        length = Int32.Parse(field);
                        Console.WriteLine("length: " + length);

                        field = fullResponseString.Substring(fullResponseString.IndexOf("gut_symptom_diary_frequency") + 11 + 27);
                        field = field.Substring(0, field.IndexOf("}") - 1);
                        freq = field;
                        Console.WriteLine("freq: " + freq);

                        field = fullResponseString.Substring(fullResponseString.IndexOf("gut_symptom_diary_symptoms") + 11 + 26);
                        Console.WriteLine("field: " + field);
                        string extraField = field.Substring(field.IndexOf("}") - 1);
                        Console.WriteLine("extraField: " + extraField);
                        field = field.Substring(0, field.IndexOf("}") - 1);
                        Console.WriteLine("field: " + field);
                        Boolean b = true;
                        string str = "";
                        while (b)
                        {
                            if (extraField.Contains("gut_symptom_diary_symptoms"))
                            {
                                str = extraField.Substring(extraField.IndexOf("gut_symptom_diary_symptoms") + 11 + 26);
                                Console.WriteLine("str: " + str);
                                extraField = str.Substring(str.IndexOf("}") - 1);
                                Console.WriteLine("extraField1_2: " + extraField);
                                field += ", " + str.Substring(0, str.IndexOf("}") - 1);
                                Console.WriteLine("field: " + field);

                            }
                            else b = false;
                        }
                        Console.WriteLine("field: " + field);
                        symptoms = field;
                        Console.WriteLine("symptoms: " + symptoms);




                        //field = rS2.Substring(rS2.IndexOf("gut_symptom_diary_other_symptoms") + 11 + 32);
                        //field = field.Substring(0, field.IndexOf("}") - 1);
                        //other = field;
                        //Console.WriteLine("othersymptoms: " + other);

                        field = fullResponseString.Substring(fullResponseString.IndexOf("gut_symptom_diary_other_symptoms") + 11 + 32);
                        Console.WriteLine("field: " + field);
                        extraField = field.Substring(field.IndexOf("}") - 1);
                        Console.WriteLine("extraField: " + extraField);
                        field = field.Substring(0, field.IndexOf("}") - 1);
                        b = true;
                        while (b)
                        {
                            if (extraField.Contains("gut_symptom_diary_other_symptoms"))
                            {
                                str = extraField.Substring(extraField.IndexOf("gut_symptom_diary_other_symptoms") + 11 + 32);
                                Console.WriteLine("str: " + str);
                                extraField = str.Substring(str.IndexOf("}") - 1);
                                Console.WriteLine("extraField1_2: " + extraField);
                                field += ", " + str.Substring(0, str.IndexOf("}") - 1);

                            }
                            else b = false;
                        }
                        Console.WriteLine("field: " + field);
                        other = field;
                        Console.WriteLine("other: " + other);

                        //field = rS2.Substring(rS2.IndexOf("gut_symptom_diary_sleep_symptoms") + 11 + 32);
                        //field = field.Substring(0, field.IndexOf("}") - 1);
                        //sleep = field;
                        //Console.WriteLine("sleepsymptoms: " + sleep);

                        field = fullResponseString.Substring(fullResponseString.IndexOf("gut_symptom_diary_sleep_symptoms") + 11 + 32);
                        Console.WriteLine("field: " + field);
                        extraField = field.Substring(field.IndexOf("}") - 1);
                        Console.WriteLine("extraField: " + extraField);
                        field = field.Substring(0, field.IndexOf("}") - 1);
                        b = true;
                        while (b)
                        {
                            if (extraField.Contains("gut_symptom_diary_sleep_symptoms"))
                            {
                                str = extraField.Substring(extraField.IndexOf("gut_symptom_diary_sleep_symptoms") + 11 + 32);
                                Console.WriteLine("str: " + str);
                                extraField = str.Substring(str.IndexOf("}") - 1);
                                Console.WriteLine("extraField1_2: " + extraField);
                                field += ", " + str.Substring(0, str.IndexOf("}") - 1);

                            }
                            else b = false;
                        }
                        sleep = field;

                        string[] arraySymptoms = symptoms.Split(new char[] { '\u002C' }, StringSplitOptions.RemoveEmptyEntries);
                        string[] arrayOtherSymptoms = other.Split(new char[] { '\u002C' }, StringSplitOptions.RemoveEmptyEntries);
                        string[] arraySleepSymptoms = sleep.Split(new char[] { '\u002C' }, StringSplitOptions.RemoveEmptyEntries);
                        Item diary_item = new Item { Id = id, Text = name, Description = desc, Frequency = freq, Length = length, Symptoms = arraySymptoms, OtherSymptoms = arrayOtherSymptoms, SleepSymptoms = arraySleepSymptoms, startDate = time };
                        await DependencyService.Get<IDataStore<Item>>().AddItemAsync(diary_item);
                    }
                    else
                    {
                        desc = Properties.Resources.StoolDescription;
                        Console.WriteLine("stool works: " + fullResponseString);

                        string field = fullResponseString.Substring(fullResponseString.IndexOf("record_id") + 11 + 9);
                        field = field.Substring(0, field.IndexOf("}") - 1);
                        id = field;
                        Console.WriteLine("id: " + id);

                        field = fullResponseString.Substring(fullResponseString.IndexOf("stool_quality_study_name") + 11 + 24);
                        field = field.Substring(0, field.IndexOf("}") - 1);
                        name = field;
                        Console.WriteLine("name: " + name);

                        field = fullResponseString.Substring(fullResponseString.IndexOf("stool_diary_time") + 11 + 16);
                        field = field.Substring(0, field.IndexOf("}") - 1);
                        Console.WriteLine("time1: " + field);


                        replacer = field.Substring(0, field.IndexOf("-"));
                        Console.WriteLine(replacer);

                        timeYear = Int32.Parse(replacer);

                        replacer = field.Substring(5, 2);
                        Console.WriteLine(replacer);

                        timeMonth = Int32.Parse(replacer);

                        replacer = field.Substring(field.LastIndexOf("-") + 1);
                        Console.WriteLine(replacer);

                        timeDate = Int32.Parse(replacer);

                        dT = new DateTime(timeYear, timeMonth, timeDate);
                        Console.WriteLine("time4: " + dT);
                        time.Date = dT;
                        Console.WriteLine("time5: " + time.Date);

                        field = fullResponseString.Substring(fullResponseString.IndexOf("stool_quality_days") + 11 + 18);
                        field = field.Substring(0, field.IndexOf("}") - 1);
                        length = Int32.Parse(field);
                        Console.WriteLine("length: " + length);

                        field = fullResponseString.Substring(fullResponseString.IndexOf("stool_quality_questions") + 11 + 23);
                        Console.WriteLine("field: " + field);
                        string extraField = field.Substring(field.IndexOf("}") - 1);
                        Console.WriteLine("extraField: " + extraField);
                        field = field.Substring(0, field.IndexOf("}") - 1);
                        Boolean b = true;
                        string str = "";
                        while (b)
                        {
                            if (extraField.Contains("stool_quality_questions"))
                            {
                                str = extraField.Substring(extraField.IndexOf("stool_quality_questions") + 11 + 23);
                                Console.WriteLine("str: " + str);
                                extraField = str.Substring(str.IndexOf("}") - 1);
                                Console.WriteLine("extraField1_2: " + extraField);
                                field += ", " + str.Substring(0, str.IndexOf("}") - 1);

                            }
                            else b = false;
                        }
                        sqq = field;

                        string[] sqqs = sqq.Split(new char[] { '\u002C' }, StringSplitOptions.RemoveEmptyEntries);

                        Item diary_item = new Item { Id = id, Text = name, Description = desc, Length = length, startDate = time, Symptoms = sqqs };
                        await DependencyService.Get<IDataStore<Item>>().AddItemAsync(diary_item);
                    }



                }

                else if (fullResponseString.Contains("gut_symptom_diary_name"))
                {

                    desc = Properties.Resources.GutDescription;
                    Console.WriteLine("works: " + fullResponseString);

                    string field = fullResponseString.Substring(fullResponseString.IndexOf("record_id") + 11 + 9);
                    field = field.Substring(0, field.IndexOf("}") - 1);
                    id = field;
                    Console.WriteLine("id: " + id);

                    field = fullResponseString.Substring(fullResponseString.IndexOf("gut_symptom_diary_name") + 11 + 22);
                    field = field.Substring(0, field.IndexOf("}") - 1);
                    name = field;
                    Console.WriteLine("name: " + name);

                    field = fullResponseString.Substring(fullResponseString.IndexOf("gut_diary_time") + 11 + 14);
                    field = field.Substring(0, field.IndexOf("}") - 1);
                    Console.WriteLine("time1: " + field);

                    replacer = field.Substring(0, field.IndexOf("-"));
                    Console.WriteLine(replacer);

                    timeYear = Int32.Parse(replacer);

                    replacer = field.Substring(5, 2);
                    Console.WriteLine(replacer);

                    timeMonth = Int32.Parse(replacer);

                    replacer = field.Substring(field.LastIndexOf("-") + 1);
                    Console.WriteLine(replacer);

                    timeDate = Int32.Parse(replacer);

                    dT = new DateTime(timeYear, timeMonth, timeDate);
                    Console.WriteLine("time4: " + dT);
                    time.Date = dT;
                    Console.WriteLine("time5: " + time.Date);

                    field = fullResponseString.Substring(fullResponseString.IndexOf("gut_symptom_diary_length") + 11 + 24);
                    field = field.Substring(0, field.IndexOf("}") - 1);
                    length = Int32.Parse(field);
                    Console.WriteLine("length: " + length);

                    field = fullResponseString.Substring(fullResponseString.IndexOf("gut_symptom_diary_frequency") + 11 + 27);
                    field = field.Substring(0, field.IndexOf("}") - 1);
                    freq = field;
                    Console.WriteLine("freq: " + freq);

                    field = fullResponseString.Substring(fullResponseString.IndexOf("gut_symptom_diary_symptoms") + 11 + 26);
                    Console.WriteLine("field: " + field);
                    string extraField = field.Substring(field.IndexOf("}") - 1);
                    Console.WriteLine("extraField: " + extraField);
                    field = field.Substring(0, field.IndexOf("}") - 1);
                    Console.WriteLine("field: " + field);
                    Boolean b = true;
                    string str = "";
                    while (b)
                    {
                        if (extraField.Contains("gut_symptom_diary_symptoms"))
                        {
                            str = extraField.Substring(extraField.IndexOf("gut_symptom_diary_symptoms") + 11 + 26);
                            Console.WriteLine("str: " + str);
                            extraField = str.Substring(str.IndexOf("}") - 1);
                            Console.WriteLine("extraField1_2: " + extraField);
                            field += ", " + str.Substring(0, str.IndexOf("}") - 1);
                            Console.WriteLine("field: " + field);

                        }
                        else b = false;
                    }
                    Console.WriteLine("field: " + field);
                    symptoms = field;
                    Console.WriteLine("symptoms: " + symptoms);




                    //field = rS2.Substring(rS2.IndexOf("gut_symptom_diary_other_symptoms") + 11 + 32);
                    //field = field.Substring(0, field.IndexOf("}") - 1);
                    //other = field;
                    //Console.WriteLine("othersymptoms: " + other);

                    field = fullResponseString.Substring(fullResponseString.IndexOf("gut_symptom_diary_other_symptoms") + 11 + 32);
                    Console.WriteLine("field: " + field);
                    extraField = field.Substring(field.IndexOf("}") - 1);
                    Console.WriteLine("extraField: " + extraField);
                    field = field.Substring(0, field.IndexOf("}") - 1);
                    b = true;
                    while (b)
                    {
                        if (extraField.Contains("gut_symptom_diary_other_symptoms"))
                        {
                            str = extraField.Substring(extraField.IndexOf("gut_symptom_diary_other_symptoms") + 11 + 32);
                            Console.WriteLine("str: " + str);
                            extraField = str.Substring(str.IndexOf("}") - 1);
                            Console.WriteLine("extraField1_2: " + extraField);
                            field += ", " + str.Substring(0, str.IndexOf("}") - 1);

                        }
                        else b = false;
                    }
                    Console.WriteLine("field: " + field);
                    other = field;
                    Console.WriteLine("other: " + other);

                    //field = rS2.Substring(rS2.IndexOf("gut_symptom_diary_sleep_symptoms") + 11 + 32);
                    //field = field.Substring(0, field.IndexOf("}") - 1);
                    //sleep = field;
                    //Console.WriteLine("sleepsymptoms: " + sleep);

                    field = fullResponseString.Substring(fullResponseString.IndexOf("gut_symptom_diary_sleep_symptoms") + 11 + 32);
                    Console.WriteLine("field: " + field);
                    extraField = field.Substring(field.IndexOf("}") - 1);
                    Console.WriteLine("extraField: " + extraField);
                    field = field.Substring(0, field.IndexOf("}") - 1);
                    b = true;
                    while (b)
                    {
                        if (extraField.Contains("gut_symptom_diary_sleep_symptoms"))
                        {
                            str = extraField.Substring(extraField.IndexOf("gut_symptom_diary_sleep_symptoms") + 11 + 32);
                            Console.WriteLine("str: " + str);
                            extraField = str.Substring(str.IndexOf("}") - 1);
                            Console.WriteLine("extraField1_2: " + extraField);
                            field += ", " + str.Substring(0, str.IndexOf("}") - 1);

                        }
                        else b = false;
                    }

                    string[] arraySymptoms = symptoms.Split(new char[] { '\u002C' }, StringSplitOptions.RemoveEmptyEntries);
                    string[] arrayOtherSymptoms = other.Split(new char[] { '\u002C' }, StringSplitOptions.RemoveEmptyEntries);
                    string[] arraySleepSymptoms = sleep.Split(new char[] { '\u002C' }, StringSplitOptions.RemoveEmptyEntries);
                    Item diary_item = new Item { Id = id, Text = name, Description = desc, Frequency = freq, Length = length, Symptoms = arraySymptoms, OtherSymptoms = arrayOtherSymptoms, SleepSymptoms = arraySleepSymptoms, startDate = time };
                    await DependencyService.Get<IDataStore<Item>>().AddItemAsync(diary_item);




                }
                else if (fullResponseString.Contains("stool_quality_study_name"))
                {
                    desc = Properties.Resources.StoolDescription;
                    Console.WriteLine("stool works: " + fullResponseString);

                    string field = fullResponseString.Substring(fullResponseString.IndexOf("record_id") + 11 + 9);
                    field = field.Substring(0, field.IndexOf("}") - 1);
                    id = field;
                    Console.WriteLine("id: " + id);

                    field = fullResponseString.Substring(fullResponseString.IndexOf("stool_quality_study_name") + 11 + 24);
                    field = field.Substring(0, field.IndexOf("}") - 1);
                    name = field;
                    Console.WriteLine("name: " + name);

                    field = fullResponseString.Substring(fullResponseString.IndexOf("stool_diary_time") + 11 + 16);
                    field = field.Substring(0, field.IndexOf("}") - 1);
                    Console.WriteLine("time1: " + field);


                    replacer = field.Substring(0, field.IndexOf("-"));
                    Console.WriteLine(replacer);

                    timeYear = Int32.Parse(replacer);

                    replacer = field.Substring(5, 2);
                    Console.WriteLine(replacer);

                    timeMonth = Int32.Parse(replacer);

                    replacer = field.Substring(field.LastIndexOf("-") + 1);
                    Console.WriteLine(replacer);

                    timeDate = Int32.Parse(replacer);

                    dT = new DateTime(timeYear, timeMonth, timeDate);
                    Console.WriteLine("time4: " + dT);
                    time.Date = dT;
                    Console.WriteLine("time5: " + time.Date);

                    field = fullResponseString.Substring(fullResponseString.IndexOf("stool_quality_days") + 11 + 18);
                    field = field.Substring(0, field.IndexOf("}") - 1);
                    length = Int32.Parse(field);
                    Console.WriteLine("length: " + length);

                    field = fullResponseString.Substring(fullResponseString.IndexOf("stool_quality_questions") + 11 + 23);
                    Console.WriteLine("field: " + field);
                    string extraField = field.Substring(field.IndexOf("}") - 1);
                    Console.WriteLine("extraField: " + extraField);
                    field = field.Substring(0, field.IndexOf("}") - 1);
                    Boolean b = true;
                    string str = "";
                    while (b)
                    {
                        if (extraField.Contains("stool_quality_questions"))
                        {
                            str = extraField.Substring(extraField.IndexOf("stool_quality_questions") + 11 + 23);
                            Console.WriteLine("str: " + str);
                            extraField = str.Substring(str.IndexOf("}") - 1);
                            Console.WriteLine("extraField1_2: " + extraField);
                            field += ", " + str.Substring(0, str.IndexOf("}") - 1);

                        }
                        else b = false;
                    }
                    sqq = field;

                    string[] sqqs = sqq.Split(new char[] { '\u002C' }, StringSplitOptions.RemoveEmptyEntries);

                    Item diary_item = new Item { Id = id, Text = name, Description = desc, Length = length, startDate = time, Symptoms = sqqs };
                    await DependencyService.Get<IDataStore<Item>>().AddItemAsync(diary_item);
                }
                else Console.WriteLine("noworks: " + fullResponseString);

                
            }






            Boolean foundEmails = true;
            string rS1 = "";
            string rS2 = "";
            int kierros = 0;
            while (foundEmails)
            {
                kierros++;
                Console.WriteLine("round " + kierros);
                if (leftOverResponseString.Contains(".com"))
                {
                    rS2 = leftOverResponseString;
                    rS1 = leftOverResponseString.Substring(leftOverResponseString.IndexOf("part_emails") + 22);
                    Console.WriteLine("RS1: " + rS1);
                    leftOverResponseString = rS1.Substring(rS1.IndexOf(".com\"}") + 4);
                    Console.WriteLine("RESPONSE STRING3_loop: " + leftOverResponseString);
                    emailList = rS1.Substring(0, rS1.IndexOf(".com\"}") + 4);
                    Console.WriteLine("RESPONSE STRING2_loop: " + emailList);


                    if (//s == "bill@gmail.com"
                    emailList.Contains(logInEmail)
                    )
                    {
                        //create new item to items in MockDataStore based on the responseString parameters;

                        string id = "";
                        string name = "";
                        DatePicker time = new DatePicker();
                        time.Date = DateTime.Now;
                        DateTime dT = time.Date;
                        int length = 0;
                        string freq = "";
                        string symptoms = "";
                        string other = "";
                        string sleep = "";
                        string desc = "";
                        string replacer = "";
                        int timeDate = 0;
                        int timeMonth = 0;
                        int timeYear = 0;
                        if (rS2.Contains("gut_symptom_diary_name") && rS2.Contains("stool_quality_study_name"))
                        {
                            int gut = rS2.IndexOf("gut_symptom_diary_name");
                            int stool = rS2.IndexOf("stool_quality_study_name");
                            Console.WriteLine("gut: " + gut + "stool: " + stool);
                            if (gut < stool)
                            {
                                desc = Properties.Resources.GutDescription;
                                Console.WriteLine("works: " + rS2);

                                string field = rS2.Substring(rS2.IndexOf("record_id") + 11 + 9);
                                field = field.Substring(0, field.IndexOf("}") - 1);
                                id = field;
                                Console.WriteLine("id: " + id);

                                field = rS2.Substring(rS2.IndexOf("gut_symptom_diary_name") + 11 + 22);
                                field = field.Substring(0, field.IndexOf("}") - 1);
                                name = field;
                                Console.WriteLine("name: " + name);

                                field = rS2.Substring(rS2.IndexOf("gut_diary_time") + 11 + 14);
                                field = field.Substring(0, field.IndexOf("}") - 1);
                                Console.WriteLine("time1: " + field);

                                replacer = field.Substring(0, field.IndexOf("-"));
                                Console.WriteLine(replacer);

                                timeYear = Int32.Parse(replacer);

                                replacer = field.Substring(5, 2);
                                Console.WriteLine(replacer);

                                timeMonth = Int32.Parse(replacer);

                                replacer = field.Substring(field.LastIndexOf("-") + 1);
                                Console.WriteLine(replacer);

                                timeDate = Int32.Parse(replacer);

                                dT = new DateTime(timeYear, timeMonth, timeDate);
                                Console.WriteLine("time4: " + dT);
                                time.Date = dT;
                                Console.WriteLine("time5: " + time.Date);

                                field = rS2.Substring(rS2.IndexOf("gut_symptom_diary_length") + 11 + 24);
                                field = field.Substring(0, field.IndexOf("}") - 1);
                                length = Int32.Parse(field);
                                Console.WriteLine("length: " + length);

                                field = rS2.Substring(rS2.IndexOf("gut_symptom_diary_frequency") + 11 + 27);
                                field = field.Substring(0, field.IndexOf("}") - 1);
                                freq = field;
                                Console.WriteLine("freq: " + freq);

                                field = rS2.Substring(rS2.IndexOf("gut_symptom_diary_symptoms") + 11 + 26);
                                Console.WriteLine("field: " + field);
                                string extraField = field.Substring(field.IndexOf("}") - 1);
                                Console.WriteLine("extraField: " + extraField);
                                field = field.Substring(0, field.IndexOf("}") - 1);
                                Console.WriteLine("field: " + field);
                                Boolean b = true;
                                string str = "";
                                while (b)
                                {
                                    if (extraField.Contains("gut_symptom_diary_symptoms"))
                                    {
                                        str = extraField.Substring(extraField.IndexOf("gut_symptom_diary_symptoms") + 11 + 26);
                                        Console.WriteLine("str: " + str);
                                        extraField = str.Substring(str.IndexOf("}") - 1);
                                        Console.WriteLine("extraField1_2: " + extraField);
                                        field += ", " + str.Substring(0, str.IndexOf("}") - 1);
                                        Console.WriteLine("field: " + field);

                                    }
                                    else b = false;
                                }
                                Console.WriteLine("field: " + field);
                                symptoms = field;
                                Console.WriteLine("symptoms: " + symptoms);




                                //field = rS2.Substring(rS2.IndexOf("gut_symptom_diary_other_symptoms") + 11 + 32);
                                //field = field.Substring(0, field.IndexOf("}") - 1);
                                //other = field;
                                //Console.WriteLine("othersymptoms: " + other);

                                field = rS2.Substring(rS2.IndexOf("gut_symptom_diary_other_symptoms") + 11 + 32);
                                Console.WriteLine("field: " + field);
                                extraField = field.Substring(field.IndexOf("}") - 1);
                                Console.WriteLine("extraField: " + extraField);
                                field = field.Substring(0, field.IndexOf("}") - 1);
                                b = true;
                                while (b)
                                {
                                    if (extraField.Contains("gut_symptom_diary_other_symptoms"))
                                    {
                                        str = extraField.Substring(extraField.IndexOf("gut_symptom_diary_other_symptoms") + 11 + 32);
                                        Console.WriteLine("str: " + str);
                                        extraField = str.Substring(str.IndexOf("}") - 1);
                                        Console.WriteLine("extraField1_2: " + extraField);
                                        field += ", " + str.Substring(0, str.IndexOf("}") - 1);

                                    }
                                    else b = false;
                                }
                                Console.WriteLine("field: " + field);
                                other = field;
                                Console.WriteLine("other: " + other);

                                //field = rS2.Substring(rS2.IndexOf("gut_symptom_diary_sleep_symptoms") + 11 + 32);
                                //field = field.Substring(0, field.IndexOf("}") - 1);
                                //sleep = field;
                                //Console.WriteLine("sleepsymptoms: " + sleep);

                                field = rS2.Substring(rS2.IndexOf("gut_symptom_diary_sleep_symptoms") + 11 + 32);
                                Console.WriteLine("field: " + field);
                                extraField = field.Substring(field.IndexOf("}") - 1);
                                Console.WriteLine("extraField: " + extraField);
                                field = field.Substring(0, field.IndexOf("}") - 1);
                                b = true;
                                while (b)
                                {
                                    if (extraField.Contains("gut_symptom_diary_sleep_symptoms"))
                                    {
                                        str = extraField.Substring(extraField.IndexOf("gut_symptom_diary_sleep_symptoms") + 11 + 32);
                                        Console.WriteLine("str: " + str);
                                        extraField = str.Substring(str.IndexOf("}") - 1);
                                        Console.WriteLine("extraField1_2: " + extraField);
                                        field += ", " + str.Substring(0, str.IndexOf("}") - 1);

                                    }
                                    else b = false;
                                }
                                Console.WriteLine();
                                Console.WriteLine();
                                Console.WriteLine();

                                string[] arraySymptoms = symptoms.Split(new char[] { '\u002C' }, StringSplitOptions.RemoveEmptyEntries);
                                string[] arrayOtherSymptoms = other.Split(new char[] { '\u002C' }, StringSplitOptions.RemoveEmptyEntries);
                                string[] arraySleepSymptoms = sleep.Split(new char[] { '\u002C' }, StringSplitOptions.RemoveEmptyEntries);
                                Item diary_item = new Item { Id = id, Text = name, Description = desc, Frequency = freq, Length = length, Symptoms = arraySymptoms, OtherSymptoms = arrayOtherSymptoms, SleepSymptoms = arraySleepSymptoms, startDate = time };
                                await DependencyService.Get<IDataStore<Item>>().AddItemAsync(diary_item);
                            }
                            else
                            {
                                desc = Properties.Resources.StoolDescription;
                                Console.WriteLine("stool works: " + rS2);

                                string field = rS2.Substring(rS2.IndexOf("record_id") + 11 + 9);
                                field = field.Substring(0, field.IndexOf("}") - 1);
                                id = field;
                                Console.WriteLine("id: " + id);

                                field = rS2.Substring(rS2.IndexOf("stool_quality_study_name") + 11 + 24);
                                field = field.Substring(0, field.IndexOf("}") - 1);
                                name = field;
                                Console.WriteLine("name: " + name);

                                field = rS2.Substring(rS2.IndexOf("stool_diary_time") + 11 + 16);
                                field = field.Substring(0, field.IndexOf("}") - 1);
                                Console.WriteLine("time1: " + field);


                                replacer = field.Substring(0, field.IndexOf("-"));
                                Console.WriteLine(replacer);

                                timeYear = Int32.Parse(replacer);

                                replacer = field.Substring(5, 2);
                                Console.WriteLine(replacer);

                                timeMonth = Int32.Parse(replacer);

                                replacer = field.Substring(field.LastIndexOf("-") + 1);
                                Console.WriteLine(replacer);

                                timeDate = Int32.Parse(replacer);

                                dT = new DateTime(timeYear, timeMonth, timeDate);
                                Console.WriteLine("time4: " + dT);
                                time.Date = dT;
                                Console.WriteLine("time5: " + time.Date);

                                field = rS2.Substring(rS2.IndexOf("stool_quality_days") + 11 + 18);
                                field = field.Substring(0, field.IndexOf("}") - 1);
                                length = Int32.Parse(field);
                                Console.WriteLine("length: " + length);

                                field = rS2.Substring(rS2.IndexOf("stool_quality_questions") + 11 + 23);
                                Console.WriteLine("field: " + field);
                                string extraField = field.Substring(field.IndexOf("}") - 1);
                                Console.WriteLine("extraField: " + extraField);
                                field = field.Substring(0, field.IndexOf("}") - 1);
                                Boolean b = true;
                                string str = "";
                                while (b)
                                {
                                    if (extraField.Contains("stool_quality_questions"))
                                    {
                                        str = extraField.Substring(extraField.IndexOf("stool_quality_questions") + 11 + 23);
                                        Console.WriteLine("str: " + str);
                                        extraField = str.Substring(str.IndexOf("}") - 1);
                                        Console.WriteLine("extraField1_2: " + extraField);
                                        field += ", " + str.Substring(0, str.IndexOf("}") - 1);

                                    }
                                    else b = false;
                                }
                                string sqq = field;

                                string[] sqqs = sqq.Split(new char[] { '\u002C' }, StringSplitOptions.RemoveEmptyEntries);

                                Item diary_item = new Item { Id = id, Text = name, Description = desc, Length = length, startDate = time, Symptoms = sqqs };
                                await DependencyService.Get<IDataStore<Item>>().AddItemAsync(diary_item);
                            }



                        }

                        else if (rS2.Contains("gut_symptom_diary_name"))
                        {

                            desc = Properties.Resources.GutDescription;
                            Console.WriteLine("works: " + rS2);

                            string field = rS2.Substring(rS2.IndexOf("record_id") + 11 + 9);
                            field = field.Substring(0, field.IndexOf("}") - 1);
                            id = field;
                            Console.WriteLine("id: " + id);

                            field = rS2.Substring(rS2.IndexOf("gut_symptom_diary_name") + 11 + 22);
                            field = field.Substring(0, field.IndexOf("}") - 1);
                            name = field;
                            Console.WriteLine("name: " + name);

                            field = rS2.Substring(rS2.IndexOf("gut_diary_time") + 11 + 14);
                            field = field.Substring(0, field.IndexOf("}") - 1);
                            Console.WriteLine("time1: " + field);

                            replacer = field.Substring(0, field.IndexOf("-"));
                            Console.WriteLine(replacer);

                            timeYear = Int32.Parse(replacer);

                            replacer = field.Substring(5, 2);
                            Console.WriteLine(replacer);

                            timeMonth = Int32.Parse(replacer);

                            replacer = field.Substring(field.LastIndexOf("-") + 1);
                            Console.WriteLine(replacer);

                            timeDate = Int32.Parse(replacer);

                            dT = new DateTime(timeYear, timeMonth, timeDate);
                            Console.WriteLine("time4: " + dT);
                            time.Date = dT;
                            Console.WriteLine("time5: " + time.Date);

                            field = rS2.Substring(rS2.IndexOf("gut_symptom_diary_length") + 11 + 24);
                            field = field.Substring(0, field.IndexOf("}") - 1);
                            length = Int32.Parse(field);
                            Console.WriteLine("length: " + length);

                            field = rS2.Substring(rS2.IndexOf("gut_symptom_diary_frequency") + 11 + 27);
                            field = field.Substring(0, field.IndexOf("}") - 1);
                            freq = field;
                            Console.WriteLine("freq: " + freq);

                            field = rS2.Substring(rS2.IndexOf("gut_symptom_diary_symptoms") + 11 + 26);
                            Console.WriteLine("field: " + field);
                            string extraField = field.Substring(field.IndexOf("}") -1);
                            Console.WriteLine("extraField: " + extraField);
                            field = field.Substring(0, field.IndexOf("}") -1);
                            Console.WriteLine("field: " + field);
                            Boolean b = true;
                            string str = "";
                            while (b)
                            {
                                if (extraField.Contains("gut_symptom_diary_symptoms"))
                                {
                                    str = extraField.Substring(extraField.IndexOf("gut_symptom_diary_symptoms") + 11 + 26);
                                    Console.WriteLine("str: " + str);
                                    extraField = str.Substring(str.IndexOf("}") - 1);
                                    Console.WriteLine("extraField1_2: " + extraField);
                                    field += ", " + str.Substring(0, str.IndexOf("}") - 1);
                                    Console.WriteLine("field: " + field);

                                }
                                else b = false;
                            }
                            Console.WriteLine("field: " + field);
                            symptoms = field;
                            Console.WriteLine("symptoms: " + symptoms);


                            

                            //field = rS2.Substring(rS2.IndexOf("gut_symptom_diary_other_symptoms") + 11 + 32);
                            //field = field.Substring(0, field.IndexOf("}") - 1);
                            //other = field;
                            //Console.WriteLine("othersymptoms: " + other);

                            field = rS2.Substring(rS2.IndexOf("gut_symptom_diary_other_symptoms") + 11 + 32);
                            Console.WriteLine("field: " + field);
                            extraField = field.Substring(field.IndexOf("}") - 1);
                            Console.WriteLine("extraField: " + extraField);
                            field = field.Substring(0, field.IndexOf("}") - 1);
                            b = true;
                            while (b)
                            {
                                if (extraField.Contains("gut_symptom_diary_other_symptoms"))
                                {
                                    str = extraField.Substring(extraField.IndexOf("gut_symptom_diary_other_symptoms") + 11 + 32);
                                    Console.WriteLine("str: " + str);
                                    extraField = str.Substring(str.IndexOf("}") - 1);
                                    Console.WriteLine("extraField1_2: " + extraField);
                                    field += ", " + str.Substring(0, str.IndexOf("}") - 1);

                                }
                                else b = false;
                            }
                            Console.WriteLine("field: " + field);
                            other = field;
                            Console.WriteLine("other: " + other);

                            //field = rS2.Substring(rS2.IndexOf("gut_symptom_diary_sleep_symptoms") + 11 + 32);
                            //field = field.Substring(0, field.IndexOf("}") - 1);
                            //sleep = field;
                            //Console.WriteLine("sleepsymptoms: " + sleep);

                            field = rS2.Substring(rS2.IndexOf("gut_symptom_diary_sleep_symptoms") + 11 + 32);
                            Console.WriteLine("field: " + field);
                            extraField = field.Substring(field.IndexOf("}") - 1);
                            Console.WriteLine("extraField: " + extraField);
                            field = field.Substring(0, field.IndexOf("}") - 1);
                            b = true;
                            while (b)
                            {
                                if (extraField.Contains("gut_symptom_diary_sleep_symptoms"))
                                {
                                    str = extraField.Substring(extraField.IndexOf("gut_symptom_diary_sleep_symptoms") + 11 + 32);
                                    Console.WriteLine("str: " + str);
                                    extraField = str.Substring(str.IndexOf("}") - 1);
                                    Console.WriteLine("extraField1_2: " + extraField);
                                    field += ", " + str.Substring(0, str.IndexOf("}") - 1);

                                }
                                else b = false;
                            }
                            Console.WriteLine("field: " + field);
                            sleep = field;
                            Console.WriteLine("sleep: " + sleep);

                            string[] arraySymptoms = symptoms.Split(new char[] { '\u002C' }, StringSplitOptions.RemoveEmptyEntries);
                            string[] arrayOtherSymptoms = other.Split(new char[] { '\u002C' }, StringSplitOptions.RemoveEmptyEntries);
                            string[] arraySleepSymptoms = sleep.Split(new char[] { '\u002C' }, StringSplitOptions.RemoveEmptyEntries);
                            Item diary_item = new Item { Id = id, Text = name, Description = desc, Frequency = freq, Length = length, Symptoms = arraySymptoms, OtherSymptoms = arrayOtherSymptoms, SleepSymptoms = arraySleepSymptoms, startDate = time };
                            await DependencyService.Get<IDataStore<Item>>().AddItemAsync(diary_item);




                        }

                        else if (rS2.Contains("stool_quality_study_name"))
                        {
                            desc = Properties.Resources.StoolDescription;
                            Console.WriteLine("stool works: " + rS2);

                            string field = rS2.Substring(rS2.IndexOf("record_id") + 11 + 9);
                            field = field.Substring(0, field.IndexOf("}") - 1);
                            id = field;
                            Console.WriteLine("id: " + id);

                            field = rS2.Substring(rS2.IndexOf("stool_quality_study_name") + 11 + 24);
                            field = field.Substring(0, field.IndexOf("}") - 1);
                            name = field;
                            Console.WriteLine("name: " + name);

                            field = rS2.Substring(rS2.IndexOf("stool_diary_time") + 11 + 16);
                            field = field.Substring(0, field.IndexOf("}") - 1);
                            Console.WriteLine("time1: " + field);


                            replacer = field.Substring(0, field.IndexOf("-"));
                            Console.WriteLine(replacer);

                            timeYear = Int32.Parse(replacer);

                            replacer = field.Substring(5, 2);
                            Console.WriteLine(replacer);

                            timeMonth = Int32.Parse(replacer);

                            replacer = field.Substring(field.LastIndexOf("-") + 1);
                            Console.WriteLine(replacer);

                            timeDate = Int32.Parse(replacer);

                            dT = new DateTime(timeYear, timeMonth, timeDate);
                            Console.WriteLine("time4: " + dT);
                            time.Date = dT;
                            Console.WriteLine("time5: " + time.Date);

                            field = rS2.Substring(rS2.IndexOf("stool_quality_days") + 11 + 18);
                            field = field.Substring(0, field.IndexOf("}") - 1);
                            length = Int32.Parse(field);
                            Console.WriteLine("length: " + length);

                            field = rS2.Substring(rS2.IndexOf("stool_quality_questions") + 11 + 23);
                            Console.WriteLine("field: " + field);
                            string extraField = field.Substring(field.IndexOf("}") - 1);
                            Console.WriteLine("extraField: " + extraField);
                            field = field.Substring(0, field.IndexOf("}") - 1);
                            Boolean b = true;
                            string str = "";
                            while (b)
                            {
                                if (extraField.Contains("stool_quality_questions"))
                                {
                                    str = extraField.Substring(extraField.IndexOf("stool_quality_questions") + 11 + 23);
                                    Console.WriteLine("str: " + str);
                                    extraField = str.Substring(str.IndexOf("}") - 1);
                                    Console.WriteLine("extraField1_2: " + extraField);
                                    field += ", " + str.Substring(0, str.IndexOf("}") - 1);

                                }
                                else b = false;
                            }
                            string sqq = field;

                            string[] sqqs = sqq.Split(new char[] { '\u002C' }, StringSplitOptions.RemoveEmptyEntries);

                            Item diary_item = new Item { Id = id, Text = name, Description = desc, Length = length, startDate = time, Symptoms = sqqs };
                            await DependencyService.Get<IDataStore<Item>>().AddItemAsync(diary_item);
                        }
                        else Console.WriteLine("noworks: " + rS2);


                    }
                }
                else foundEmails = false;
            }
            //responseString2 = responseString2.Remove(responseString2.Length - 3);
            Console.WriteLine("RESPONSE STRING2_3: " + emailList);


            string[] arrayResponseString = emailList.Split(new char[] { '\u002C' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < arrayResponseString.Length; i++)
            {
                arrayResponseString[i] = arrayResponseString[i].Trim();
            }
            int emails = 0;
            foreach (string s in arrayResponseString)
            {
                emails++;
                Console.WriteLine("Email" + emails + ": " + s);
            }
            int testing = 0;
            foreach (string s in arrayResponseString)
            {
                testing++;
                
            }
            



            //var content = new FormUrlEncodedContent(values);
            //var response = await client.PostAsync("https://redcap.utu.fi/api/", content);
            //var responseString = await response.Content.ReadAsStringAsync();
        }
    }
}